<?php
class SendToFriendController extends Apps_Controller_Action_Default {
	public function indexAction() {
        $rq = $this->_request;
        $id = intval($rq->getParam('id'));
        $t  = intval($rq->getParam('t'));
        $f  = new F_SendToFriend();

        if($rq->isPost()) {
            if($f->isValid($rq->getParams())) {
                $fullName = $f->getValue('full_name');
                $email    = $f->getValue('email');
                $reEmail  = $f->getValue('reconfirm_email');
                $toEmail  = $f->getValue('to_email');
                $subject  = $f->getValue('subject');
                $message  = $f->getValue('message');
                $captcha  = $f->getValue('captcha');

                if($email == $reEmail && $fullName != '') {
                    //check captcha
                    $captchaSession = new Zend_Session_Namespace('captchaSession');
                    $captchaCodeSession = $captchaSession->captcha_code;

                    $captchaCode = md5($captcha . '760211');
                    if($captchaCodeSession != $captchaCode) {
                        $this->view->error = "Captcha_doesn't_match";
                    } else {
                        $tbVctSetting = new M_DbTable_VctSetting();
                        $vctSetting   = $tbVctSetting->getVctSetting()->query()->fetch();

                        $passwordEmailVct = $vctSetting['password_email'];
                        $passwordEmailVct = substr(substr($passwordEmailVct, 8), 0, -7); //decode

                        $mail = new Apps_PhpMailer();
                        $mail->IsSMTP();
                        $mail->IsHTML(true);
                        $mail->SMTPDebug = 2;

                        try {
                            $mail->SMTPAuth   = true;
                            $mail->CharSet    = 'UTF-8';
                            $mail->Host       = $vctSetting['host_email'];;
                            $mail->Port       = $vctSetting['port_email'];
                            $mail->SMTPSecure = $vctSetting['ssl_email'];
                            $mail->Username   = $vctSetting['email'];
                            $mail->Password   = $passwordEmailVct;


                            $title = 'detail';
                            if($t == 1) {
                                $tourModel = new M_Tour();
                                $tour = $tourModel->getTourById($id)->query()->fetch();
                                $title = trim(str_replace(' ', '-', $tour['title']));
                                $message = $message . '<br/><br/><a style="color:#F3951F;font-size:16px"
                                                            href="http://vietcentertourist.com/tour/detail/'.$id.'-'.$title .'.html">
                                                            http://vietcentertourist.com/tour/detail/'.$id.'-'.$title .'.html</a>';
                            } else if($t == 2) {
                                $cruiseModel = new M_Cruise();
                                $cruise = $cruiseModel->getCruiseInfo($id)->query()->fetch();
                                $title = trim(str_replace(' ', '-', $cruise['name']));
                                $message = $message  . '<br/><br/><a style="color:#F3951F;font-size:16px"
                                                             href="http://vietcentertourist.com/cruise/detail/'.$id.'-'.$title .'.html">
                                                             http://vietcentertourist.com/cruise/detail/'.$id.'-'.$title .'.html </a>';
                            }

                            $mail->SetFrom($vctSetting['email'], $fullName);
                            $mail->AddReplyTo($email, $fullName);

                            $mail->Subject = $subject;
                            $mail->Body = $message;

                            $validator = new Zend_Validate_EmailAddress();
                            $toEmail   = explode("\r", trim($toEmail));
                            $messageToMail = "";
                            foreach($toEmail as $k => $v) {
                                if($validator->isValid(trim($v))) {
                                    try {
                                        $mail->AddAddress($v);
                                        $mail->Send();
                                        $mail->ClearAddresses();
                                    } catch (Zend_Exception $e) {
                                        echo $e->getMessage();
                                    }
                                    $messageToMail .= $v . ','; 
                                }
                            }
                        } catch (phpmailerException $e) {
                            echo $e->errorMessage();
                        } catch (Zend_Exception $e) {
                            echo $e->getMessage();
                        }
                        
                        $message = "Your sharing offer is successfully sent to " . $messageToMail . '</br>' . $vctSetting['reply_message_send_mail'] ;
                        $this->_flashMessenger->addMessage($message);
                        $this->_redirect('/send-to-friend/index/t/'.$t.'/id'.$id);
                    }
                }
            }
        }
        $this->view->form = $f;
	}
}